function switchStyleSheet()
{
 	var i, a, main, previous;
	var title=getActiveStyleSheet();
 	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
		{
   		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
				{
					if ( a.getAttribute("title") == title ) previous = i;
					a.disabled = true;
    		}
  	}
	if ( previous+1 == i )
		{
			document.getElementsByTagName("link")[0].disabled = false;
		}
	else
		{
			document.getElementsByTagName("link")[previous+1].disabled = false;
		}
	return false;
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
	/* Enable JS-Only stuff */
	var text = document.createTextNode("Changer de style");
	var link = document.createElement("a");
	link.appendChild(text);
	link.href = "#";
	link.onclick =switchStyleSheet;
	if (link.captureEvents) link.captureEvents(Event.CLICK);
	var li = document.createElement("li");
	li.id = "switchcss";
	li.appendChild(link);
	var list = document.getElementById("linklist");
	list.insertBefore(li, list.firstChild);	

	/* Load previous style */
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);

/* restore prefs */
	var weekshift = readCookie("weekshift");
	document.getElementById("weekshift").value = weekshift ? weekshift : '00';

	var diplome = readCookie("diplome");
	document.getElementById("diplome").value = diplome ? diplome : '00';

  var groupe = readCookie("groupe");
	document.getElementById("groupe").value = ( groupe != null ) ? groupe : '' ;

	var cssid = readCookie("cssid");
	document.getElementById("cssid").value = cssid ? cssid : '01';

	var blacklist =  readCookie("blacklist");
	document.getElementById("blacklist").value = ( blacklist != null ) ? blacklist : '' ;
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
	
	createCookie("weekshift", document.getElementById("weekshift").value, 365);
	createCookie("diplome", document.getElementById("diplome").value, 365);
	createCookie("groupe", document.getElementById("groupe").value, 365);
	createCookie("cssid", document.getElementById("cssid").value, 365);
	createCookie("blacklist", document.getElementById("blacklist").value, 365);

  createCookie("style", title, 365);
}


